/**************************************************************************************************
  Filename:       bsp_storage.h
  Revised:        $Date: 2014-12-04 10:20:18 +0800 (Fri, 04 Apr 2014) $
  Revision:       $Revision: 10001 $

  Description:    This file contains the board support package of storage data.
**************************************************************************************************/

#ifndef __BSP_STORAGE_H__
#define __BSP_STORAGE_H__

#ifdef __cplusplus
extern "C"
{
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
  
#include "hal_types.h"
#include "hal_timer.h"
#include "comdef.h"

#include "OSAL.h"
#include "OSAL_Clock.h"
#include "hal_drivers.h"
#include "hal_dbflash.h"



#define HARDWARE_NOT_INITIALIZE 0x00
#define HARDWARE_INITIALIZED    0xAA

#define UDHeadLen 0x05
#define UDLen2 	0x00
#define UDLen4 	0x40
#define UDLen6 	0x80
#define UDLen8 	0xC0

#define UDTHumi 0x00
#define UDTTemp 0x01
#define UDTSlep	0x02
#define UDTGyro 0x03
#define UDTRsv2 0x04
#define UDTRsv3 0x05
#define UDTBatt 0x06
#define UDTTime 0x07

#define UDTLeds 0x08
#define UDTSets 0x09
#define UDTWarn 0x0A
#define UDTErro	0x0F

#define UDTMASK 0x0F


/***************************
 *  Not use UD structure when setting the info.
****************************/
// send data 
#define UPLOAD_PURE_DATA 0x10
#define PURE_DATA_USERINFO 0x01
#define PURE_DATA_SECUINFO 0x02
#define PURE_DATA_SETTINGINFO 0x03

#define PURE_DATA_LEN2 0x20
#define PURE_DATA_LEN4 0x40
#define PURE_DATA_LEN6 0x60
#define PURE_DATA_LEN8 0x80
#define PURE_DATA_LEN10 0xA0
#define PURE_DATA_LEN12 0xC0

// receive data
#define NOTUDMASK 0x10

#define PARTMASK 0xE0

#define SETMASK 0x0F

#define SET_DATA_LEN 12 

#define SET_CHILDNAME 0x01
#define SET_BIRTHTIME 0x02
#define SET_ALLERGY 0x03
#define SET_TOY 0x04
#define SET_FOOD 0x05
#define SET_MNAME 0x06
#define SET_MPHONE 0x07
#define SET_FNAME 0x08
#define SET_FPHONE 0x09
#define SET_POSITION 0x0A
#define SET_USERNAME 0x0B
#define SET_PASSWORD 0x0C
#define SET_ADDRESS 0x0D



#define META_DATA_NORMAL		0x00
#define META_DATA_SYNCDONE		0xAA
#define META_DATA_INTERRUPT		0x55


#define DONT_NEED_NEW_PAGE 	0x00
#define ADDR_INNER_MASK  	0x1FF
#define ADDR_LAST_PAGE   	0xFFE00
#define DUMMY_BYTE		0xFF
#define ADDR_ONE_PAGE		0x200  
 
#ifndef PAGE_SIZE
#define PAGE_SIZE 			0x108		//264
#endif


// Cmd Type
// Hardware Cmd
// Function
#define CFUN_LED1			0xA1
#define CFUN_LED2			0xA2
#define CFUN_LED3			0xA3
#define CFUN_STOP_SYNC		0xA4
#define	CFUN_SAVE_CURRENT	0xA5

// Dangerous Cmd
// Function
#define CFUN_UPDATE_ROM			0xAA
#define CFUN_DISABLE_ROM		0xBB
#define CFUN_REBOOT				0xCC
//GET
#define CGET_ITERRUPT_ADDR	0xFC	
#define CGET_FIRMWAREVER	0xFD
#define CGET_DEVICEID		0xFE
#define CGET_VERSION		0xFF

// PureData Set
#define CDAT_CHILDNAME		0x00
#define CDAT_BIRTHTIME		0x01
// #define 
#define CDAT_ALLERGEY		0x03
#define CDAT_TOY			0x04
#define CDAT_FOOD			0x05
#define CDAT_MNAME			0x06
#define CDAT_MPHONE			0x07
#define CDAT_FNAME			0x08
#define CDAT_FPHONE			0x09
#define CDAT_POSTION		0x0A
#define CDAT_USERNAME		0x0B
#define CDAT_PASSWORD		0x0C
#define CDAT_ADDRESS		0x0D

// Value Set
#define CVAL_DIAPER_INTERVAL	0x10
#define CVAL_DIAPER_NUMBER		0x11
#define CVAL_DIAPER_FACTOR		0x12
#define CVAL_SLEEP_INTERVAL		0x13
#define CVAL_SLEEP_FACTOR		0x14
#define CVAL_MOTION_INTERVAL	0x15
#define CVAL_MOTION_FACTOR		0x16
#define CVAL_BATT_INTERVAL		0x17
#define CVAL_BATT_FACTOR		0x18
#define CVAL_TIME				0x19
#define CVAL_INTERVAL 			0x1A

// Whole Page get
#define CPAG_DATAINFO			0x20
#define CPAG_USERINFO			0x21
#define CPAG_SECUINFO			0x22
#define CPAG_TIMEINFO			0x23
#define CPAG_SETTINGS			0x24
#define CPAG_HARDWARE_INFO		0x25

// Flash Data

#define CFLA_APPEND				0x30   	// Append
#define CFLA_DELETE				0x31 	// Delete
#define CFLA_CHANGE				0x32 	// Change
#define CFLA_SELECT				0x33 	// Select
 
#define PAGE_DATAINFO_LENGTH 29
#define PAGE_USERINFO_LENGTH 252
#define PAGE_SECUINFO_LENGTH 216
#define PAGE_TIMEINFO_LENGTH 4
#define PAGE_SETTINGS_LENGTH 56
#define PAGE_HARDINFO_LENGTH 29

typedef struct __data_info
{
	uint32 datasize;
	uint8 status;
	uint32 iterAddr;
	uint32 startAddr;
	uint32 endAddr;
	uint32 newAddr;
	uint32 lastUpdateTime;
	uint32 lastSyncTime;
}DataPage;

typedef struct __user_info 		//256
{
	uint8 name[24];
	uint32 age;
	uint8 toy[84];
	uint8 food[84];
	uint8 allergy[56];	
}UserPage;	//need to be auto refresh

typedef struct __secu_info		//216 Byte
{
	uint8 username[12];		//use phone
	uint8 password[36];
	uint8 fatherName[24];
	uint8 motherName[24];
	uint8 fatherPhone[12];
	uint8 motherPhone[12];
	uint8 address[96];
}SecuPage;

typedef struct __time_info
{
	uint32 time;
}TimePage;

typedef struct __setttings
{
	// uint8 setting[30];
	uint8 update_flag;
	uint32 intervalTime;
	uint32 diaper_interval;
	uint32 diaper_number;
	uint32 diaper_factor;
	uint32 sleep_interval;
	uint32 sleep_factor;
	uint32 motion_interval;
	uint32 motion_factor;
	uint32 batt_interval;
	uint32 batt_factor;
	uint32 update_interval;
	uint8 type[8];
	uint8 position[8];
}SettingsPage;

typedef struct __hardware_info
{
	uint8 flag;
	uint32 manufacture;
	uint32 version;
	uint32 firmversion;
	uint32 firstOn;
	uint32 firstSync;
	uint32 lastUpdate;
	uint32 deviceID;
}HardWareInfo;


// UnitData structure used for Storage in flash
typedef struct flashdata
{
	uint8 meta;
	uint8 data[8];
}UnitData;

// Setting Data Sync First Package 

typedef	struct _list_data
{
	uint8 uid;
	uint8 pos;
	uint8 len;
	uint8 *data;
	struct _list_data *next;
}CmdReg;

typedef struct firstpackage
{
	uint8 taskID;
	uint8 meta;
	//sub meta
	uint8 setting_type;
	uint8 method;
	uint8 len;
	uint8 randid;
	uint8 data[8];
}PACKHEAD;

typedef struct sync_data
{
	uint8 meta;
	uint8 randid;
	uint8 no[2];
	uint8 data[8];
}PACKDATA;

// Data Upload
typedef struct upload_data
{
	uint8 meta;
	uint8 randid;
	uint8 no[4];		//address
	uint8 data[8];
}DATAUPLOAD;


extern void RegCMD(uint8 uid,uint8 len,uint8 setting_type);
extern uint8 getPointerViaSettingType(uint8 setting_type,volatile uint8 *data);
extern uint8 UnregCMD(uint8 uid);


uint8 UD_init(UnitData *unit,uint8 type,uint8 *data);
uint8 UD_setLength(uint8 type,uint8 len);
uint8 UD_setType(uint8 type,uint8 sensor);
uint32 UD_write(UnitData unit,uint32 addr);
uint32 UD_read(UnitData *unit,uint32 addr);
uint8 UD_toString(UnitData unit,uint8 *string);
uint8 getUDLen(uint8 type);
uint8 getLenByTypeCode(uint8 type);
uint8 getUDLenCode(uint8 len);
uint32 bsp_write32b(uint32 addr,uint32 data);
uint32 bsp_write8b(uint32 addr, uint8 data);
uint32 bsp_write(uint32 addr,uint8 volatile *buffer,uint8 len);
//uint32 bsp_write(uint32 addr,uint8 *buffer,uint8 len);
uint32 bsp_read32b(uint32 addr,uint32 volatile *data);
uint32 bsp_read8b(uint32 addr,uint8 volatile *data);
uint32 bsp_read(uint32 addr,uint8 volatile *buffer,uint8 len);

uint8 UDHumiInit(UnitData *unit,uint8 *data);
uint8 UDSlepInit(UnitData *unit,uint8 *data);
uint8 UDRsvInit(UnitData *unit,uint8 udtype,uint8 *data);
uint8 UDBattInit(UnitData *unit,uint8 *data);
uint8 UDTimeInit(UnitData *unit,uint8 *data);
// uint8 UDWarnInit(UnitData *unit,uint8 *data);
// uint8 UDErroInit(UnitData *unit,uint8 *data);

uint8 UD_getLength(uint8 type);

/****************
*	Data Page Init and Operation
****************/
void bsp_dataPgInit(void);
void bsp_mem2dataPg(void);
void bsp_updateAddr(void);
void bsp_dataPg2Mem(void);
/****************
*	User Page Init and Operation
****************/
void bsp_userPgInit(void);
void bsp_mem2userPg(void);
void bsp_userPg2Mem(void);
/****************
*	Security Page Init and Operation
****************/
void bsp_secuInit(void);
void bsp_mem2secuPg(void);
void bsp_secuPg2Mem(void);
/****************
*	Time Page Init and Operation
****************/
void bsp_timeSave(void);
void bsp_updateTime(void);
void bsp_timePg2Mem(void);
/****************
*	Setting Page Init and Operation
****************/
void bsp_settingsInit(void);
void bsp_mem2setPg(void);
void bsp_setPg2Mem(void);
/****************
*	Hardware Page Init and Operation 
****************/
void bsp_hardwareInit(void);
void bsp_flashInitFlag(void);
void bsp_updateHardware(void);
void bsp_hwPg2Mem(void);

extern volatile DataPage _dataPg;
extern volatile UserPage _userPg;
extern volatile SecuPage _secuPg;
extern volatile TimePage _timePg;
extern volatile SettingsPage _settingsPg;
extern volatile HardWareInfo _hardPg;
extern volatile UTCTime __currentTimeUnix;

extern volatile uint32 __currentAddr;
extern volatile uint32 __iterAddr;



#ifdef __cplusplus
}
#endif
#endif


